Skip to content

Feeding MP3 audio to the MP4 muxer only while something reads MP4 - #100

Merged
wberube merged 1 commit into
OpenIPC:masterfrom
erkin-io:fix/mp4-audio-without-reader
Nov 17, 2026
Merged

wberube merged 1 commit into
OpenIPC:masterfrom
erkin-io:fix/mp4-audio-without-reader

Conversation

@erkin-io

Copy link
Copy Markdown
Contributor

What was wrong

With MP3 audio on and nobody reading /video.mp4 or recording, Divinus slowly uses up the memory of the camera. Seen on a Ruision F8 board (FH8856V200): +312 kB in 90 s at 32 kbit/s; a second board, left running for 30 hours, had its streamer at 22.8 MB (normally 3.4 MB) with 7.3 MB of memory left.

Why

aenc_thread() (src/media.c) passes every MP3 frame to mp4_ingest_audio(), which appends it to the muxer's audio buffer buf_aud. Only mp4_set_slice() empties that buffer, and it is called only from send_mp4_to_client() while an MP4 client is connected and from the recorder while it records. The MP3 encoder also runs for RTSP, RTMP and /audio.mp3, so with RTSP on (the default) and no MP4 reader, buf_aud grows for as long as the program runs.

What this changes

  • New any_http_mp4() in src/server.c (declared in server.h), the MP4-only twin of any_http_audio().
  • aenc_thread() calls mp4_ingest_audio() only when record_active() or any_http_mp4() says that someone will empty the buffer.

How it was checked

  • Built with ./build.sh fhan-v2, no new warnings; ./test.sh -a passes.
  • On a Ruision F8 board, in the firmware that normally runs on the board (which goes further: one audio queue per MP4 consumer and a 64 kB cap per queue): the streamer's memory stayed at 3036 to 3032 kB over 90 s in MP3 mode with RTSP on and no MP4 reader, against +312 kB before. This pull request is the smaller part of that change; it was built but not run on the board in this exact form.

Effect on other platforms

Shared code, every target with audio. Users who read /video.mp4 or record: unchanged while they are there (audio is fed as before). The first fragment after an MP4 client connects carries only the audio from that moment on, instead of whatever had piled up before.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Stop buffering MP3 audio when no MP4 consumer is active

🐞 Bug fix 🕐 10-20 Minutes

Grey Divider

AI Description

• Feed MP3 frames to the MP4 muxer only while an MP4 client or recording is active.
• Prevent the muxer's audio buffer from growing indefinitely when MP3 serves other outputs.
Diagram

graph TD
  Encoder["MP3 encoder"] --> Gate{"MP4 consumer?"} -->|yes| Buffer["MP4 audio buffer"] --> Fragment["MP4 fragment"]
  Gate -->|no| Skip["Skip MP4 ingest"]
  Clients["MP4 clients"] --> Gate
  Recorder["Active recording"] --> Gate
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Bound the MP4 audio buffer
  • ➕ Limits memory use even if a consumer stops draining audio.
  • ➖ Requires a policy for dropping audio and may still retain stale frames between consumers.
2. Use bounded per-consumer audio queues
  • ➕ Isolates clients and permits explicit limits for slow readers.
  • ➖ Substantially expands muxer state and synchronization for a narrower idle-buffering bug.

Recommendation: Use the proposed demand gate for this fix: it prevents idle accumulation without changing active MP4 or recording paths. Bounded queues are worth considering separately if slow consumers also cause growth.

Files changed (3) +19 / -3

Bug fix (3) +19 / -3
media.cGate MP4 audio ingestion on an active consumer +9/-3

Gate MP4 audio ingestion on an active consumer

• The audio encoder now sends MP3 frames to the MP4 muxer only during recording or while an HTTP MP4 client is present. MP3 delivery to other outputs remains outside the gate.

src/media.c

server.cDetect connected HTTP MP4 clients +9/-0

Detect connected HTTP MP4 clients

• Adds a mutex-protected scan of the client registry that reports whether a connected client has the MP4 stream type.

src/server.c

server.hExpose the HTTP MP4 client check +1/-0

Expose the HTTP MP4 client check

• Declares the new client-presence function for use by the audio encoder.

src/server.h

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Tip of the day
💡 Did you know, you can enable the Remediation agent and Qodo fixes findings in a dedicated fix PR

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@wberube
wberube merged commit 98af42c into OpenIPC:master Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants